package clock;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Calendar;

class Bell {
    String bellPath="/audio/aMusic/";
    String bellName = "1.wav";
    AudioStream audioStream;

    public Bell() {
        try {
            InputStream input = getClass().getResourceAsStream(bellPath+bellName); //创建输入流
            if (input != null) {
                audioStream = new AudioStream(input);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Bell(String bellName) {
        this.bellName = bellName;
        try {
            InputStream input = getClass().getResourceAsStream(bellPath+bellName); //创建输入流
            if (input != null) {
                audioStream = new AudioStream(input);//用输入流input创建音频流对象
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Bell(String bellPath, String bellName) {
        this.bellPath = bellPath;
        this.bellName = bellName;
        try {
            InputStream input = getClass().getResourceAsStream(bellPath+bellName); //创建输入流
            if (input != null) {
                audioStream = new AudioStream(input);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void startBell(){
        AudioPlayer.player.start(audioStream);// 用静态成员player.start播放音乐
    }

    public void stopBell(){
        AudioPlayer.player.stop(audioStream);// 用静态成员player.stop停止播放音乐
    }

}

class Alarm {
    private boolean isWork;
    private boolean alarmOn;
    private int alarmHour = 8;
    private int alarmMinute = 0;
    private static int nums = 0;
    private String name = "Alarm" + nums;
    private Bell alarmBell = new Bell();

    public Alarm() {
        this.isWork = false;//停止响铃
        this.alarmOn = true;//开启闹铃
        nums += 1;
    }

    public Alarm(int alarmHour, int alarmMinute) {
        this();
        this.alarmHour = alarmHour;
        this.alarmMinute = alarmMinute;
    }

    public Alarm(int alarmHour, int alarmMinute, String name) {
        this(alarmHour, alarmMinute);
        this.name = name;
    }

    public boolean isWork() {//获取响铃状况
        return isWork;
    }

    public void setWork(boolean isWork) {//设置响铃状况
        this.isWork = isWork;
    }

    public boolean isAlarmOn() {
        return alarmOn;
    }

    public void setAlarmOn(boolean alarmOn) {
        this.alarmOn = alarmOn;
    }

    @Override
    public String toString() {
        return "Alarm{" + "name='" + name + '\'' + ", alarmOn=" + alarmOn + ", isWork=" + isWork + ", alarmTime=" + alarmHour / 10 + alarmHour % 10 + ":" + alarmMinute / 10 + alarmMinute % 10 + '}';
    }

    public void startAlarm(){
        alarmBell.startBell();
    }

    public void stopAlarm(){
        alarmBell.stopBell();
    }

}

class ClockPanel extends JPanel {
    int centerX = 300;//面板中心X坐标
    int centerY = 300;//面板中心Y坐标
    int secondHand = 180;//秒针长度
    int minuteHand = 155;//分针长度
    int hourHand = 115;//时针长度
    boolean loaded = false;
    Calendar now;//当前时间
    BufferedImage clockImage;//钟面图像

    private final Font f1 = new Font("微软雅黑", Font.PLAIN, 15);
    private final Font f2 = new Font("微软雅黑", Font.BOLD, 15);

    public ClockPanel() {
        URL url = getClass().getResource("/images/s41.png");//获取钟面图像的位置
        try {
            if (url != null) {
                clockImage = ImageIO.read(url);//载入图片
                loaded = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void drawHand(Graphics2D g) {
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DATE);
        int week = now.get(Calendar.DAY_OF_WEEK);
        int second = now.get(Calendar.SECOND);//获取当前时间秒数
        int minute = now.get(Calendar.MINUTE);//获取当前时间分钟数
        int hour = now.get(Calendar.HOUR_OF_DAY);//获取当前时间小时数
        double x, y;
        double angle;
//        System.out.printf("%d-%d-%d\n", year, month, day);
        //绘制时针
        //计算时针的角度，3点为0度，12点为90度
        angle = (Math.PI * 2) * hour / 12 + (30 * Math.PI / 180) * minute / 60 + (0.5 * Math.PI / 180) * second / 60 - Math.PI / 2;
        x = hourHand * Math.cos(-angle);//计算时针外端点x坐标
        y = hourHand * Math.sin(-angle);//计算时针外端点y坐标
        g.setColor(Color.BLACK);//设置指针颜色为黑色
        g.setStroke(new BasicStroke(5.5f));//设置指针宽度
        g.drawLine(centerX, centerY, (int) (centerX + x), (int) (centerY - y));//绘制指针前端
        g.drawLine(centerX, centerY, (int) (centerX - 0.1 * x), (int) (centerY + 0.1 * y));//绘制指针尾部

        //绘制分针
        angle = (Math.PI * 2) * minute / 60 + (6 * Math.PI / 180) * second / 60 - Math.PI / 2;//
        x = (int) (minuteHand * Math.cos(-angle));//
        y = (int) (minuteHand * Math.sin(-angle));//
        g.setStroke(new BasicStroke(3.3f));//
        g.drawLine(centerX, centerY, (int) (centerX + x), (int) (centerY - y));//
        g.drawLine(centerX, centerY, (int) (centerX - 0.1 * x), (int) (centerY + 0.1 * y));//

        //绘制秒针
        angle = (2 * Math.PI) * second / 60 - Math.PI / 2;//
        x = (int) (secondHand * Math.cos(-angle));//
        y = (int) (secondHand * Math.sin(-angle));//
        g.setColor(Color.RED);//设置指针颜色为红色
        g.setStroke(new BasicStroke(2.0f));//
        g.drawLine(centerX, centerY, (int) (centerX + x), (int) (centerY - y));//
        g.drawLine(centerX, centerY, (int) (centerX - 0.1 * x), (int) (centerY + y * 0.1));//

        //绘制固定指针的圆点
        g.setColor(Color.BLACK);
        g.fillOval(centerX - 10 / 2, centerY - 10 / 2, 10, 10);
        g.setColor(Color.white);
        g.fillOval(centerX - 1, centerY - 1, 2, 2);
    }

    protected void drawDial(Graphics2D g) {
        int width = 450, height = 450;
        String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2"};

        //绘制表盘的轮廓
        g.setPaint(new GradientPaint(5, 40, Color.blue, 15, 50, Color.yellow, true));
        g.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
        g.drawOval(centerX - width / 2, centerY - height / 2, width, height);
        //画60个点和数字
        g.setColor(Color.black);
        g.setFont(f2);
        for (int i = 0; i < 60; i++) {
            double angle = i * 2 * Math.PI / 60;
            double x = 0.5 * (width - 20) * Math.cos(angle);
            double y = 0.5 * (height - 20) * Math.sin(angle);
            if (i % 5 == 0) {
                g.fillOval((int) (x + centerX - 5), (int) (y + centerY - 5), 10, 10);
                x = 0.5 * (width - 60) * Math.cos(angle);
                y = 0.5 * (height - 60) * Math.sin(angle);
                g.drawString(nums[i / 5], (int) (x + centerX - 15 / 2), (int) (y + centerY + 15 / 2));
            } else {
                g.fillOval((int) (x + centerX - 2.5), (int) (y + centerY - 2.5), 5, 5);
            }
        }
    }

    public void playAudio(String name) {
        try {
            String audioPath = "/audio/lyq/" + name;
            InputStream input = getClass().getResourceAsStream(audioPath); //创建输入流
            if (input != null) {
                AudioStream audioStream = new AudioStream(input);//用输入流input创建音频流对象
                AudioPlayer.player.start(audioStream);// 用静态成员player.start播放音乐
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        now = Calendar.getInstance();//获取当期时间的Calendar对象
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//打开抗锯齿

        //绘制表盘
        if (loaded) {//如果成功加载表盘图片则绘制图片，否侧绘制表盘
            g.drawImage(clockImage, 50, 50, null);//在钟面上绘制图片
        } else {
            drawDial(g2d);
        }

        drawHand(g2d);//绘制表针

        if (now.get(Calendar.SECOND) == 0 && now.get(Calendar.MINUTE) == 0) {
            int index = now.get(Calendar.HOUR_OF_DAY);
            String name = String.format("%d%d.wav", index / 10, index % 10);
            playAudio(name);
        }
    }

}


public class Clock {
    JFrame frame = new JFrame("时钟");//窗口
    ClockPanel clockPanel = new ClockPanel();//面板
    Alarm[] alarms = new Alarm[6];
    int alarmNum = 0;
    int maxNum = 6;
    Timer timer;

    public Clock() {
        frame.add(clockPanel);//将面板添加到窗口
        frame.setBounds(200, 100, 600, 600);//设置窗口左上角的坐标和窗口的大小
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口的关闭操作
        frame.setResizable(false);//设置窗口大小不能改变
        frame.setVisible(true);//设置窗口可见
        Bell bell = new Bell();
        bell.startBell();

//        timer = new Timer(1000, new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                clockPanel.repaint();
//                System.out.println("Hello");
//            }
//        });
        timer = new Timer(1000, e -> {
            clockPanel.repaint();

        });

        timer.start();
    }
}
